import datetime

from django import forms

from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

class RenewBookForm(forms.Form):
    renewal_date = forms.DateField(help_text="Enter a date between now and 4 weeks (default 3).")

    def clean_renewal_date(self):
        data = self.cleaned_data['renewal_date']

        # Check if a date is not in the past.
        if data < datetime.date.today():
            raise ValidationError(_('Invalid date - renewal in past'))

        # Check if a date is in the allowed range (+4 weeks from today).
        if data > datetime.date.today() + datetime.timedelta(weeks=4):
            raise ValidationError(_('Invalid date - renewal more than 4 weeks ahead'))

        # Remember to always return the cleaned data.
        return data

# catalog/forms.py
from django import forms
from .models import Book

class BookCreateForm(forms.ModelForm):
    """创建新书的表单"""
    class Meta:
        model = Book
        fields = ['title', 'author', 'isbn', 'summary', 'genre']  # 需包含的字段

class BookUpdateForm(forms.ModelForm):
    """编辑书籍的表单"""
    class Meta:
        model = Book
        fields = ['title', 'author', 'isbn', 'summary', 'genre']  # 同上

class BookDeleteForm(forms.Form):
    """删除书籍的确认表单（简化版，仅用于确认）"""
    confirm = forms.BooleanField(
        required=True,
        label='我确认要删除这本书',
        widget=forms.CheckboxInput
    )